<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ MCP SDK/MCP Server SDK 整合演示</title>
        <meta name="description" content="轻量级的 Java 8 Model Context Protocol (MCP) SDK. MCP Server SDK 整合演示"/>
        <meta name="keywords" content="mcp, mcp sdk, java mcp, mcp client, mcp server, java8 mcp, MCP Server SDK 整合演示"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="/asset/style/main.css"/>
        <link rel="icon" type="image/png" href="/asset/favicon.ico"/>
        <script src="/asset/common.js"></script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-mcp">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-mcp">Gitcode</a>
                    |
                    <a href="/">英文版本</a>
                </div>
                <h1>AJ MCP SDK</h1>
                <h3>用户手册</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                    <li>
                        <a href="/architecture/index-cn">架构原理</a>
                    </li>
                </ul>
                <h3>MCP 客户端 SDK</h3>
                <ul>
                    <li>
                        <a href="/client/setup-client-cn">安装与配置</a>
                    </li>
                    <li>
                       <a href="/client/initialization-cn">初始化</a>
                    </li>
                    <li>
                       <a href="/client/resource-cn">Resources 用法</a>
                    </li>
                     <li>
                       <a href="/client/prompt-cn">Prompts 用法</a>
                    </li>
                     <li>
                       <a href="/client/tool-cn">Tools 用法</a>
                    </li>
                </ul>
                <h3>MCP 服务端 SDK</h3>
                <ul>
                      <li>
                           <a href="/server/usage-server-cn">Server SDK 用法指南</a>
                      </li>
                      <li>
                           <a href="/server/res-server-cn">Resources 用法</a>
                      </li>
                      <li>
                           <a href="/server/pro-server-cn">Prompts 用法</a>
                      </li>
                      <li>
                             <a href="/server/tool-server-cn">Tools 用法</a>
                       </li>
                      <li>
                         <a href="/server/sample-cn">整合例子</a>
                      </li>
                </ul>

                <h3>其他</h3>
                <ul>
                    <li><a href="/misc/versions">版本记录</a></li>
                    <li><a href="/misc/contact-cn">联系方式</a></li>
                </ul>
            </menu>
            <article>
                <h1>MCP 服务器 SDK 集成示例</h1>
<p>本项目的源代码仓库包含两个集成示例：一个是带有简单 MCP 服务的独立 Tomcat 服务器，另一个是带有 MCP 服务的 Spring Boot 应用程序。</p>
<h2>Tomcat 应用集成</h2>
<p>Tomcat 部署展示了一个完整的服务器搭建模式：</p>
<pre><code class="language-java">package com.foo.myapp;


import com.ajaxjs.mcp.server.McpServer;
import com.ajaxjs.mcp.server.ServerSse;
import com.ajaxjs.mcp.server.common.ServerConfig;
import com.ajaxjs.mcp.server.feature.FeatureMgr;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import java.io.File;

public class StandaloneTomcat {
    public static void main(String[] args) throws Exception {
        FeatureMgr mgr = new FeatureMgr();
        mgr.init(&quot;com.foo.myapp&quot;);

        McpServer server = new McpServer();
        ServerSse serverSse = new ServerSse(server);
        server.setTransport(serverSse);

        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setName(&quot;MY_MCP_Server&quot;);
        serverConfig.setVersion(&quot;1.0&quot;);
        server.setServerConfig(serverConfig);

        server.start();

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);

        // Set base directory (for temp files)
        tomcat.setBaseDir(&quot;.&quot;);

        // Create a context (no web.xml required)
        String contextPath = &quot;&quot;;
        String docBase = new File(&quot;.&quot;).getAbsolutePath();
        Context context = tomcat.addContext(contextPath, docBase);

        // Register SSE servlet
        SseServlet sseServlet = new SseServlet(serverSse);
        Tomcat.addServlet(context, &quot;sseServlet&quot;, sseServlet);
        context.addServletMappingDecoded(&quot;/sse&quot;, &quot;sseServlet&quot;);

        // Register Message servlet
        Tomcat.addServlet(context, &quot;messageServlet&quot;, new MessageServlet(serverSse));
        context.addServletMappingDecoded(&quot;/message&quot;, &quot;messageServlet&quot;);

        // Configure connectionTimeout and keepAliveTimeout
        Connector connector = tomcat.getConnector();
        connector.setProperty(&quot;connectionTimeout&quot;, &quot;60000&quot;); // 20 seconds
        connector.setProperty(&quot;keepAliveTimeout&quot;, &quot;60000&quot;); // 30 seconds
        connector.setProperty(&quot;maxKeepAliveRequests&quot;, &quot;100&quot;); // Optional: Max requests per connection

        tomcat.start();
        tomcat.getServer().await();
    }
}
</code></pre>
<h2>Spring 应用集成</h2>
<p>Spring 配置演示了依赖注入的设置方式，见 <code>Config.java:12-29</code>，其中 ServerSse Bean 按相同模式配置，但由 Spring 容器管理。</p>
<pre><code class="language-java">package com.foo.myapp;

import com.ajaxjs.mcp.server.McpServer;
import com.ajaxjs.mcp.server.ServerSse;
import com.ajaxjs.mcp.server.common.ServerConfig;
import com.ajaxjs.mcp.server.feature.FeatureMgr;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public ServerSse serverSse() {
        FeatureMgr mgr = new FeatureMgr();
        mgr.init(&quot;com.foo.myapp&quot;);

        McpServer server = new McpServer();
        ServerSse serverSse = new ServerSse(server);
        server.setTransport(serverSse);

        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setName(&quot;MY_MCP_Server&quot;);
        serverConfig.setVersion(&quot;1.0&quot;);
        server.setServerConfig(serverConfig);

        server.start();

        return serverSse;
    }
}
</code></pre>
<h2>设计说明</h2>
<p>对于基于 SSE 的 MCP 服务器，有两个端点需要了解：</p>
<ul>
<li><strong>SSE Url</strong>：这是客户端最初连接的端点，在初始化时使用。在此初始化过程中，服务器会返回一个 POST Url（第二个端点）。</li>
<li><strong>POST Url</strong>：这是 MCP 业务的实际端点，客户端将请求发送到该端点，服务器也会通过该端点返回响应。它同样是一个 SSE 端点。</li>
</ul>

                <div id="comment-holder">
                </div>
            </article>
        </div>
        <footer>
             AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
             frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
             <br />
             <br />
             Copyright © 2025 Frank Cheung. All rights reserved.
         </footer>
    </body>
</html>